package com.ohuang.apkMerge

import org.dom4j.Attribute
import org.dom4j.io.SAXReader

fun readManifest(path: String) {
    var saxReader = SAXReader()
    var read = saxReader.read(path)
    var rootElement = read.rootElement

    var data = rootElement.attribute("package").data
    println("package=$data")
    rootElement.elements().forEach {
        if (it.name.equals("application")) {
            it.elements().forEach {

                if (it.name.equals("provider")) {
                    println(it.attribute("authorities").data)
                }
            }
        }

    }
}

fun mergeManifest(path: String, path2: String) {
    var saxReader = SAXReader()
    var read = saxReader.read(path)
    var rootElement = read.rootElement
    var packge = rootElement.attribute("package").data as String

    var saxReader2 = SAXReader()
    var read2 = saxReader2.read(path2)
    var rootElement2 = read2.rootElement
    var applictionElement2 = rootElement2.element("application")
    var packge2 = rootElement2.attribute("package").data as String

    rootElement.elements().forEach {
        if (it.name.equals("application")) {
            it.elements().forEach { appe ->
                var createCopy = appe.createCopy()
                if (createCopy.name.equals("provider")) {
                    tryCatch(false) {
                        var data = createCopy.attribute("authorities").data as String
                        var replace = data.replace(packge, packge2)
                        createCopy.setAttributeValue("authorities", replace)
                    }
                } else if (createCopy.name.equals("activity")) {
                    tryCatch(false) {
                        var data = createCopy.attribute("taskAffinity").data as String
                        var replace = data.replace(packge, packge2)
                        createCopy.setAttributeValue("taskAffinity", replace)
                    }
                }
                applictionElement2.add(createCopy)
            }
            var attributes = applictionElement2.attributes()
            it.attributes()?.forEach { att ->
                tryCatch({
                    val attribute = applictionElement2.attribute(att.name)
                    var data = attribute.data
                }) {
                    val clone = att.clone() as Attribute
                    clone.parent = null
                    attributes.add(clone)
                }


            }
            applictionElement2.setAttributes(attributes)
        } else {
            var createCopy = it.createCopy()
            if (it.name.equals("uses-permission") || it.name.equals("permission")) {
                var data = createCopy.attribute("name").data as String
                var replace = data.replace(packge, packge2)
                createCopy.setAttributeValue("name", replace)
            }
            rootElement2.add(createCopy)

        }

    }
    rootElement2.remove(applictionElement2)
    rootElement2.add(applictionElement2)

    saveXml(path2, read2)
}